<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Consultas.aspx.cs" Inherits="Consultas" MasterPageFile="~/MstConsultas.master"%>
<%@ MasterType VirtualPath="~/MstConsultas.master" %>

<asp:Content ID="cntConsultas" ContentPlaceHolderID="cphPrincipal" runat="Server">
    <script type="text/javascript">
        function fc_Trim(pstrInput) {
                var i;
                var vstrTemp = '';
                var j = 0;
                var cadena = pstrInput;
                
                for(i=0; i<cadena.length; )
                    {
                            if(cadena.charAt(i)==" ")
                                    cadena=cadena.substring(i+1, cadena.length);
                            else
                                    break;
                    }

                    for(i=cadena.length-1; i>=0; i=cadena.length-1)
                    {
                            if(cadena.charAt(i)==" ")
                                    cadena=cadena.substring(0,i);
                            else
                                    break;
                    }
                return cadena;

        }
        function fc_Validar()
        {
            var strMsgError = "";
            
            if (document.getElementById("<%= this.ddlBD.ClientID %>").value == "" )
            {
                strMsgError += "- Debe seleccionar una BD.\n";
            }
            
            if (fc_Trim(document.getElementById("<%= this.txtComando.ClientID %>").value) == "" )
            {
                strMsgError += "- Debe ingresar el comando a ejecutar.\n";
            }
            
            if ( fc_Trim(strMsgError) != "" )
            {
                alert(strMsgError);
                return false;
            }
            
            return true;
        }
        
    </script>
    <div>
        <table>
            <tr>
                <td><asp:Label ID="lblBD" Text="Base de Datos:" runat="server"></asp:Label></td>
                <td><asp:DropDownList ID="ddlBD" runat="server" OnSelectedIndexChanged="ddlBD_SelectedIndexChanged" AutoPostBack="true" Width="120px"></asp:DropDownList></td>
                <td>
                    <asp:UpdatePanel ID="upDdlTipo" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlTipo" runat="server" OnSelectedIndexChanged="ddlTipo_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged" />
                        </Triggers>
                    </asp:UpdatePanel>
                </td>
                <td>
                     <table width="200">
                        <tr>
                            <td>
                                <asp:UpdatePanel ID="upDdlTabla" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlTablas" runat="server" Width="100%"  OnSelectedIndexChanged="ddlTablas_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged" />
                                        <asp:AsyncPostBackTrigger ControlID="ddlTipo" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:UpdatePanel ID="upDdlProcedimiento" runat="server">
                                    <ContentTemplate>
                                        <asp:DropDownList ID="ddlProcedimientos" runat="server" Width="100%"  OnSelectedIndexChanged="ddlProcedimientos_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged" />
                                        <asp:AsyncPostBackTrigger ControlID="ddlTipo" EventName="SelectedIndexChanged" />
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                    </table>
                </td>
                <td><asp:Label ID="lblSentecias" Text="Sentecias:" runat="server"></asp:Label></td>
                <td>
                    <asp:UpdatePanel ID="upDdlSentencia" runat="server">
                        <ContentTemplate>
                            <asp:DropDownList ID="ddlSentencia" runat="server" OnSelectedIndexChanged="ddlSentencia_SelectedIndexChanged" AutoPostBack="true"/>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged" />
                            <asp:AsyncPostBackTrigger ControlID="ddlTipo" EventName="SelectedIndexChanged" />
                            <asp:AsyncPostBackTrigger ControlID="ddlTablas" EventName="SelectedIndexChanged" />
                        </Triggers>
                    </asp:UpdatePanel>
                </td>
                <td>
                    <asp:UpdatePanel ID="upBtnGenerar" runat="server">
                        <ContentTemplate>
                            <asp:Button ID="btnGenerar" Text="Generar" runat="server" OnClick="btnGenerar_Click" />
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged" />
                            <asp:AsyncPostBackTrigger ControlID="ddlTipo" EventName="SelectedIndexChanged" />
                            <asp:AsyncPostBackTrigger ControlID="ddlTablas" EventName="SelectedIndexChanged" />
                            <asp:AsyncPostBackTrigger ControlID="ddlSentencia" EventName="SelectedIndexChanged" />
                        </Triggers>                    
                    </asp:UpdatePanel>
                            
                </td>
            </tr>
            <tr >
                <td colspan="4">
                    <table width="100%">
                        <tr>
                            <td style="width: 500px">Comando:</td>
                        </tr>
                        <tr>
                            <td style="width: 500px">
                                <asp:UpdatePanel ID="upTxtComando" runat="server">
                                    <ContentTemplate>
                                        <asp:TextBox id="txtComando" runat="server" Width="100%" Wrap="false" Rows="10" TextMode="MultiLine" Height="153px"></asp:TextBox> 
                                    </ContentTemplate>
                                    <Triggers>
                                        <asp:AsyncPostBackTrigger ControlID="btnGenerar" EventName="Click"></asp:AsyncPostBackTrigger>
                                    </Triggers>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" style="width: 500px">
                                <asp:Button ID="btnConsultar" runat="server" Text="Consultar" OnClick="btnConsultar_Click" OnClientClick="javascript: return fc_Validar();" />
                                <asp:Button ID="btnEjecutar" runat="server" Text="Ejecutar" OnClick="btnEjecutar_Click" OnClientClick="javascript: return fc_Validar();" />
                            </td>
                        </tr>
                     </table>
                </td>
                <td colspan="3" style="height: 402px">
                    <asp:UpdatePanel ID="upGvColumnas" runat="server">
                        <ContentTemplate>
                            <table width="100%">
                                <tr>
                                    <td style="width: 350px">
                                        <asp:GridView id="gvColumnas" runat="server" Visible="false" DataKeyNames="NombreColumna" AutoGenerateColumns="false" SkinID="Grilla">
                                            <Columns>
                                                <asp:BoundField HeaderText="Columna" DataField="NombreColumna" />
                                                <asp:BoundField HeaderText="TipoColumna" DataField="TipoColumna" />
                                                <asp:TemplateField HeaderText="Sel.">
                                                    <ItemTemplate>
                                                        <asp:CheckBox ID="chkColumna" runat="server" Checked="true"/>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 350px; height: 153px" align=left>
                                        <asp:TextBox id="lblProcedimiento" runat="server" Width="100%" Wrap="false" Rows="10" TextMode="Multiline" Visible="false" ReadOnly="true"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="ddlBD" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
                            <asp:AsyncPostBackTrigger ControlID="ddlTipo" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
                            <asp:AsyncPostBackTrigger ControlID="ddlTablas" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
                            <asp:AsyncPostBackTrigger ControlID="ddlProcedimientos" EventName="SelectedIndexChanged"></asp:AsyncPostBackTrigger>
                        </Triggers>
                    </asp:UpdatePanel>
                </td>
            </tr>
            <tr>
                <td colspan="7" align="center">
                    <asp:UpdatePanel ID="upDivResultado" runat="server">
                        <ContentTemplate>
                            <div id="divResultado" runat="server"></div>
                        </ContentTemplate>
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnConsultar" EventName="Click" />
                            <asp:AsyncPostBackTrigger ControlID="btnEjecutar" EventName="Click" />
                        </Triggers>
                    </asp:UpdatePanel>
                </td>                
            </tr>
        </table>
    </div>
</asp:Content>